package com.example.springmvc.controller;

import com.example.springmvc.model.UserInfo;
import com.sun.org.glassfish.gmbal.ParameterNames;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/4/21 19:22
 */
@Controller
@Slf4j
@RequestMapping("/user")
@ResponseBody
public class UserController {
    //默认是支持post和get方法
    @RequestMapping("/login")
    public String login(String username,String password){
        return username+":"+password;
    }
    //仅支持post方法 1
    @RequestMapping(value = "/login1",method = RequestMethod.POST)
    public String login1(String username,String password ){
        return username + " : " +password;
    }
    //仅支持post方法 2
    @PostMapping("/login2")
    public String login2(String username,String password ){
        return username + " : " +password;
    }
    //对应的仅支持Get方法 也有两个
    //仅支持GET方法 1
    @RequestMapping(value = "/login3",method = RequestMethod.GET)
    public String login3(String username,String password ){
        return username + " : " +password;
    }
    //仅支持GET方法 2
    @GetMapping("/login4")
    public String login4(String username,String password ){
        return username + " : " +password;
    }

    //传递参数
    //传递单个参数
    @GetMapping("/parameter")
    public String parameter(String username ){
        return username ;
    }
    //传递多个参数
    @GetMapping("/parameter1")
    public String parameter1(String username,String password,String id ){
        return username + ":" +password+":"+id;
    }
    //参数过多，就需要传递一个对象
    //请求的查询串必须和 UserInfo的属性名一样
    @GetMapping("/parameter2")
    public String parameter3(UserInfo userInfo){
        return userInfo.getUsername() + ":" +
                userInfo.getSex()+":"+userInfo.getId()+":"+userInfo.getPassword();
    }
    //参数重命名
    @GetMapping("/parameter3")
    public String parameter3(@RequestParam("name")String username){
        return username ;
    }
    //使用@RequestParam默认必须传入他修饰的参数，否则就会报错
    //于是可以通过参数配置让他变为 非必传参数
    @GetMapping("/parameter4")
    public String parameter4(@RequestParam(value = "name",required = false)String username) {
        return username;
    }
    //传入JSON数据为参数
    @RequestMapping("/json")
    public String getJson(@RequestBody UserInfo userInfo){
        log.info(userInfo.toString());
        return userInfo.toString();
    }
    //从URL中获取参数
    @RequestMapping("/parm/{username}/{password}")
    public String getUrlParm(@PathVariable String username,@PathVariable String password){
        return username + " | "+password;
    }
    //上传文件
    //实现不同平台的日志读取  配置文件的处理  UUID
    @Value("${img.path}")
    String imgPath ;
    @RequestMapping("/img")
    public boolean upImg(Integer id , @RequestPart("myfile") MultipartFile file){
        boolean res = false;
        //解决两个问题  路径（包括生产环境、开发环境）  文件名（UUID唯一名+文件类型）
        //路径 通过配置文件
        log.error(imgPath);
        //文件名
        String fileName = file.getOriginalFilename();
        //拿到后缀
        fileName = fileName.substring(fileName.lastIndexOf("."));
        //UUID 获得 唯一文件名
        fileName = UUID.randomUUID()+fileName;
        log.error(fileName);
        try {
            file.transferTo( new File(imgPath+fileName));
            res= true;
        } catch (IOException e) {
            log.error("图片上传失败 : "+e.getMessage());
        }
        return res;
    }

    //获取Cookie
    //Servlet时代方法
    @RequestMapping("/getcookie")
    public void getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie : cookies){
            log.info(cookie.getName() + " | "+cookie.getValue());
        }
    }
    //简单的获取
    @RequestMapping("/getcookie1")
    public String  getCookie1(@CookieValue("liudaoniuniu") String value ,@CookieValue("liuda066") String value1){
        return value+"|"+value1;
    }
    //获取Session
    //获取之前先设置Session
    @RequestMapping("/setsession")
    public boolean setSession(HttpServletRequest request){
        boolean res = false;
        HttpSession session = request.getSession(true);
        session.setAttribute("userName","liudaoniuniu");
        res = true;
        return res;
    }
    //Servlet时代方法
    @RequestMapping("/getsession")
    public String getSession(HttpServletRequest request){
        String res = null;
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute("userName") != null){
            res= (String) session.getAttribute("userName");
        }
        return res;
    }
    //简单的获取
    @RequestMapping("/getsession1")
    public String getSession1(@SessionAttribute(value = "userName",required = false) String userName){
        return userName;
    }


    //获取Header
    //Servlet时代方法
    @RequestMapping("/gethead")
    public String getHead(HttpServletRequest request){
        return request.getHeader("User-Agent");
    }
    //简单的获取
    @RequestMapping("/gethead1")
    public String getHead1(@RequestHeader("User-Agent") String userAgent){
        return userAgent;
    }
    //返回数据
}
